package servlet;


import util.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet("/infoServlet.do")
public class infoServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String userIdString = (String) session.getAttribute("user_id");
        Integer userId = null;
        if (userIdString != null) {
            userId = Integer.parseInt(userIdString);
        }

        if (userId != null) {
            List<Map<String, Object>> searchResults = performSearch(userId); // 调用搜索逻辑
            request.setAttribute("searchResults", searchResults);

            // 在这里不要向客户端输出任何内容，包括空格、换行等
            // 保证在转发之前没有任何输出
            request.getRequestDispatcher("user/info.jsp").forward(request, response);
        } else {
            // 如果无法从会话中获取到 user_id，可以进行相应的处理，比如跳转到登录页面
            response.sendRedirect("home/login.jsp");
            // 或者直接返回错误信息
            // response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "User not logged in");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doGet(req, resp);

    }

    private List<Map<String, Object>> performSearch(int userId) {
        List<Map<String, Object>> userList = new ArrayList<>();
        JDBCUtil jdbcUtil = new JDBCUtil();

        try {
            String sql = "SELECT * FROM 02_38_yangyang WHERE user_id = ?";
            ResultSet resultSet = jdbcUtil.prepareQuery(sql, userId);

            while (resultSet.next()) {
                String gender = resultSet.getString("gender");
                String profession = resultSet.getString("profession");
                String favor = resultSet.getString("favor");
                String description = resultSet.getString("description");
                String username = resultSet.getString("username");
                int state = resultSet.getInt("state");
                int role = resultSet.getInt("role");

                // 检查是否存在 null 值，避免空指针异常
                if (gender != null && profession != null && favor != null && description != null) {
                    Map<String, Object> user = Map.of(
                            "id", userId,
                            "username", username,
                            "gender", gender,
                            "profession", profession,
                            "favor", favor,
                            "description", description,
                            "state", state,
                            "role", role
                    );

                    userList.add(user);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            // 可以返回一个空列表或者抛出异常，视情况而定
        } finally {
            jdbcUtil.closeConnection(); // 关闭数据库连接
        }

        return userList;
    }
}



